﻿using System.Windows.Controls;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 注意：主、客观题均以“扩展式”组合其它类型公式而成，故实现代码可合并。
        /// </summary>
        private static void InsertExam(string examItemXmlText)
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Expression curEx = Globals.selectedEditor.SelectedExpression;

            Expressions.SubPanel sPanel = curEx as Expressions.SubPanel;
            if (sPanel != null)
            {
                if (sPanel.BasePanel.Children.Count <= 0)
                {
                    System.Windows.MessageBox.Show("发生意外错误，插入试题的目标子面板中没有任何公式！",
                        Globals.appName, System.Windows.MessageBoxButton.OK,
                        System.Windows.MessageBoxImage.Warning);
                    return;
                }
                curEx = sPanel.BasePanel.Children[sPanel.BasePanel.Children.Count - 1] as Expressions.Expression;
            }

            if (curEx == null)
            {
                System.Windows.MessageBox.Show("请先选取一个公式作为插入新试题的目标位置！",
                        Globals.appName, System.Windows.MessageBoxButton.OK,
                        System.Windows.MessageBoxImage.Warning);
                return;
            }

            if (curEx.Parent is StackPanel)
            {
                curEx.XmlData.InsertXml(examItemXmlText);

                StackPanel spanel = curEx.Parent as StackPanel;
                LunarPresentation.Expressions.Expander newExpression =
                    new LunarPresentation.Expressions.Expander(curEx.RootPanel, curEx.ParentPanel, curEx.Editor);
                spanel.Children.Insert(spanel.Children.IndexOf(curEx) + 1, newExpression);
                newExpression.XmlData = curEx.XmlData.NextSibling;

                ModifingItem mi = new ModifingItem();
                Action action = new Action(newExpression.ExID, spanel.Children.IndexOf(newExpression),
                   newExpression.GetType().Name, "InsertExpression", null, newExpression.XmlData.OuterXml,
                   ActionType.Insert, newExpression.ParentPanel.ExID);

                Action actionIndent = new Action(newExpression.ExID, spanel.Children.IndexOf(newExpression),
                    newExpression.GetType().Name, "Indent", newExpression.Indent.ToString(), curEx.Indent.ToString());


                newExpression.Indent = curEx.Indent;// 插入新公式时自动同步缩进。

                mi.Add(action); mi.Add(actionIndent);
                newExpression.Editor.Do(mi);

                Globals.selectedEditor.SelectedExpression = newExpression;
                Globals.selectedEditor.IsModified = true;
            }
        }

        /// <summary>
        /// 插入客观题。
        /// </summary>
        public static void InsertObjectiveItem()
        {
            InsertExam(LunarPresentation.Properties.Resources.ObjectiveItem);
        }

        /// <summary>
        /// 插入主观题。
        /// </summary>
        public static void InsertSubjectiveItem()
        {
            InsertExam(LunarPresentation.Properties.Resources.SubjectiveItem);
        }
    }
}
